package loading
{
   import command.DoTweenMax;
   
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.geom.Point;
   import flash.text.TextField;
   
   import jp.progression.casts.CastMovieClip;
   
   /**
    * Loading動畫元件範本
    * @author cjboy | cjboy1984@gmail.com
    * @usage
    // 加入loading
    new AddChild(stage, loading).execute();
    // 設百分比
    loading.perc = 100;
    // 移掉loading
    new RemoveChild(stage, loading).execute();
    */
   public class DataLoading_1 extends CastMovieClip
   {
      // fla
      public var tfPerc:TextField;
      
      // blocker
      private var blocker:Sprite = new Sprite();
      
      /* constructor */
      public function DataLoading_1(initObject:Object = null)
      {
         super(initObject);
         
         id = 'data loading 1';
         group = 'root';
         
         stop();
         
         // blocker
         addChildAt(blocker, 0);
      }
      
      // --------------------- LINE ---------------------
      
      // Percentage
      public function set perc(v:String):void
      {
         tfPerc.text = v;
      }
      
      // ################### protected ##################
      
      override protected function atCastAdded():void
      {
         // basic
         onStageResize();
         stage.addEventListener(Event.RESIZE, onStageResize);
         
         // animation
         blocker.alpha = 0.8;
         alpha = 0;
         addCommand(
            new command.DoTweenMax(this, 0.3, { alpha:1 } )
         );
      }
      
      override protected function atCastRemoved():void
      {
         // basic
         if (stage) stage.removeEventListener(Event.RESIZE, onStageResize);
         
         // animation
         addCommand(
            new DoTweenMax(this, 0.15, { alpha:0 } )
         );
      }
      
      // #################### private ###################
      
      // --------------------- LINE ---------------------
      
      private function onStageResize(e:Event = null):void
      {
         x = orgX;
         y = orgY;
         
         // blocker
         var pos:Point = globalToLocal(new Point(0, 0));
         blocker.graphics.clear();
         blocker.graphics.beginFill(0x000000);
         blocker.graphics.drawRect(pos.x, pos.y, sw, sh);
         blocker.graphics.endFill();
      }
      
      /* original x/y position */
      private function get orgX():Number { return sw / 2; }
      private function get orgY():Number { return sh / 2; }
      
      /* get stage.stageWidth/Height */
      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }
      
      // --------------------- LINE ---------------------
      
   }
   
}